package ctgu.yao.cloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ctgu.yao.cloud.dto.PageDTO;
import ctgu.yao.cloud.form.SearchForm;
import ctgu.yao.cloud.mapper.ApplicationMapper;
import ctgu.yao.cloud.po.Application;
import ctgu.yao.cloud.po.Recruitment;
import ctgu.yao.cloud.mapper.RecruitmentMapper;
import ctgu.yao.cloud.service.RecruitmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Y
 * @since 2022-08-16
 */
@Service
public class RecruitmentServiceImpl extends ServiceImpl<RecruitmentMapper, Recruitment> implements RecruitmentService {
@Autowired
private RecruitmentMapper recruitmentMapper;
@Autowired
private ApplicationMapper applicationMapper;
    //发布求职信息
    @Override
    public void save(Recruitment recruitment, Integer cuid) {
        /*System.out.println(cuid);
        System.out.println(recruitment)*/;
        recruitment.setCuid(cuid);
        int insert = this.recruitmentMapper.insert(recruitment);
    }
    //实现登陆人员查看自己发布的职位信息（分页）
     @Override
    public PageDTO list(Integer page, Integer size ,Integer cuid) {
            QueryWrapper<Recruitment> queryWrapper =new QueryWrapper<>();
            queryWrapper.eq("cuid",cuid);
            Page<Recruitment> recruitmentPage = new Page<>(page, size);
            Page<Recruitment> resultPage = this.recruitmentMapper.selectPage(recruitmentPage, queryWrapper);
            PageDTO pageDTO = new PageDTO();
            pageDTO.setTotal(resultPage.getTotal());
            pageDTO.setData(resultPage.getRecords());
            return pageDTO;
    }
    //实现求职人员查看自己发布的职位信息（分页）
    @Override
    public PageDTO list(Integer page, Integer size) {
        Page<Recruitment> recruitmentPage = new Page<>(page, size);
        Page<Recruitment> resultPage = this.recruitmentMapper.selectPage(recruitmentPage, null);
        PageDTO pageDTO = new PageDTO();
        pageDTO.setTotal(resultPage.getTotal());
        pageDTO.setData(resultPage.getRecords());
        return pageDTO;
    }

    @Override
    public PageDTO search(SearchForm searchForm) {
        Page<Recruitment> recruitmentPage=new Page<>(searchForm.getPage(),searchForm.getSize());
        Page<Recruitment> resultPage=null;
        if(searchForm.getValue1().equals("")&&searchForm.getValue2().equals("")&&searchForm.getValue3().equals("")){
            resultPage=this.recruitmentMapper.selectPage(recruitmentPage,null);
        }else{
            QueryWrapper<Recruitment> queryWrapper=new QueryWrapper<>();
            queryWrapper.like("name",searchForm.getValue1());
            queryWrapper.like("salary",searchForm.getValue2());
            queryWrapper.like("place",searchForm.getValue3());
            resultPage=this.recruitmentMapper.selectPage(recruitmentPage,queryWrapper);

        }
        PageDTO pageDTO = new PageDTO();
        pageDTO.setTotal(resultPage.getTotal());
        pageDTO.setData(resultPage.getRecords());
        return pageDTO;
    }

    @Override
    public Boolean application(Integer reid, Integer rid) {
        Application application = new Application();
        application.setReid(reid);
        application.setRid(rid);
        application.setState("0");
        int insert = this.applicationMapper.insert(application);
        if (insert != 1) return false;
        return true;
    }
}
